package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.clearofchina.core.util.HttpUtil;
import com.clearofchina.core.util.StringUtil;
import org.springframework.util.DigestUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author gxing
 * @desc TODO
 * @date 2023/9/1
 */
public class MainTest5 {

    public static String url = "https://hdeps.91160.com/hdeps/servlet/channel/api/v1/queryOpenIdByCardNo?";

    public static void main(String[] args) throws IOException {
        HashMap<String, String> paramMap = new HashMap<>();
        String signKey = "24d54926e526157a541728a90488c942";

        paramMap.put("request_id", "53034");
        paramMap.put("channel_id", "szdxzyy");
        paramMap.put("unit_id", "100015578");
        paramMap.put("card_no", "0000400279");

        Set<String> keys = paramMap.keySet();
        Set<String> paramKeys = new TreeSet<>(keys);
        System.out.println(paramKeys);
        StringBuilder sb1 = new StringBuilder();
        for (String key : paramKeys) {
            if(StringUtil.isBlank(paramMap.get(key))){
                continue;
            }
            sb1.append(key).append("=").append(paramMap.get(key)).append("&") ;
        }
        StringBuilder sb2 = new StringBuilder(sb1);
        sb2.append("key=").append(signKey);
        String sign = DigestUtils.md5DigestAsHex(sb2.toString().getBytes());
        System.out.println(sb2.toString());
        System.out.println(sign);
        sb1.append("sign=").append(sign);
        System.out.println(sb1);
        url = url + sb1.toString();
        String result = HttpUtil.post(url, null);
        System.out.println(result);

        WxOpenid160Response wxOpenid160Response = JSON.parseObject(result, WxOpenid160Response.class);
        JSONObject jsonObject = JSON.parseObject(result);
        System.out.println(wxOpenid160Response);
        System.out.println(jsonObject);

        if(wxOpenid160Response.success()){
            String openId = wxOpenid160Response.getData().get(0).getOpenId();
            System.out.println(openId);
        }


    }
}
